/* 
 *  jQuery register.js file
 *  
 */

$(function() {
  
  // Register form validation
  $( "#form-reg-home" ).validate({
     rules: {
        // simple rule, converted to {required:true}
        'user[username]'   : {
                                  required: true,
                                  minlength: 4,
                                  remote: {
                                            url: BASE_URL+"ajax/check_username",
                                            type: "post",
                                            data: {
                                                    username: function() {
                                                                  return $( "#reg_username" ).val();
                                                              }
                                                   }
                                            }
                              }, 
        'user[email]'      : {
                                required : true,
                                email: true,
                                remote: {
                                            url: BASE_URL+"ajax/check_email",
                                            type: "post",
                                            data: {
                                                    email: function() {
                                                                  return $( "#reg_email" ).val();
                                                              }
                                                   }
                                            }
                             },
        'user[birthdate][day]'   : "required",
        'user[birthdate][month]' : "required",
        'user[birthdate][year]'  : "required"
        },
     messages:{
         'user[username]'  : {
                               //required: "",
                               //minlength: "debe al menos 4 caracteres",
                               //remote: "nombre de usuario ya existe"
                             },
         'user[email]'     : {
                               //required: "",                              
                               //remote: "Dirección de correo ya existe"
                             },
         'user[birthdate][day]'   : "",
         'user[birthdate][month]' : "",
         'user[birthdate][year]'  : ""
        },
     submitHandler: function() {                                     
            $( "#form-reg-home" ).submit();
        },
     invalidHandler: function() { } 
    });
    
    // Register form validation // equalTo: "#password1"
   
  $( "#full-reg-form" ).validate({
     rules: {
        // simple rule, converted to {required:true}
        'user[username]'   : {
                                  required: true,
                                  minlength: 4,
                                  remote: {
                                            url: BASE_URL+"ajax/check_username",
                                            type: "post",
                                            data: {
                                                    username: function() {
                                                                  return $( "#reg_username" ).val();
                                                              }
                                                   }
                                            }
                              }, 
        'user[email]'      : {
                                required : true,
                                email: true,
                                remote: {
                                            url: BASE_URL+"ajax/check_email",
                                            type: "post",
                                            data: {
                                                    email: function() {
                                                                  return $( "#reg_email" ).val();
                                                              }
                                                   }
                                            }
                             },
        'user[password]'   : {
                                  required: true,
                                  minlength: 8
                              },
        'user[repeat_password]' : {
                                      required: true,
                                      minlength: 8,
                                      equalTo: "#password1"
                                  },
        'user[birthdate][day]'   : "required",
        'user[birthdate][month]' : "required",
        'user[birthdate][year]'  : "required",
        'user[zip_code]'         : "required",
        //'user[location]'         : "required",
        'submit_agree'           : "required"
        },
        //*
     messages:{
         'user[username]'  : {
                               required: "Pflichtfeld",
                               minlength: "Username muss mind. 4 Zeichen haben.",
                               remote: "Username bereits vorhanden."
                             },
         'user[email]'     : {
                               required: "Bitte füge die richtige Emailadresse ein.",  
                               remote: "Emailadresse bereits vorhanden, versuche bitte eine andere Emailadresse."
                             },
         'user[password]'  : {
                                required: "Pflichtfeld",
                                minlength: "Passwort muss mind. 8 Zeichen haben."
                              },
         'user[repeat_password]'  : "Pflichtfeld",
         'user[birthdate][day]'   : "Pflichtfeld",
         'user[birthdate][month]' : "Pflichtfeld",
         'user[birthdate][year]'  : "Pflichtfeld",
         'user[zip_code]'         : "Pflichtfeld" // ,
         //'user[location]'         : ""
        },
        //*/
     submitHandler: function() {                                     
            register_user();
            return false;
        },
     invalidHandler: function() { } 
    });
    
//    $("#register_submit").click(function(){
//      register_user();
//    });
    
    function register_user()
    {
        var registerData = $("#full-reg-form").serialize();
        var ajaxURL = BASE_URL + 'register/register_user';
        //alert(registerData);
        $("#register_submit").attr('disabled',true);
        $("body").css("cursor", "wait");
        // Ajax upit
        $.ajax({  
            type: "POST", 
            url: ajaxURL,
            data: registerData,
            cache: false,
            async: false,
            success: function(response){

                var parsed = JSON.parse(response);
                var html = '';                
                if(parseInt(parsed.ind) == 1)
                {
                    // Nakon registrovanja ide na stranicu profila
                    window.location = parsed.redirect_url;
                }
                else
                {      
                    html += "<p>"+parsed.msg +"</p>";
                    $('#response').show().html(html).delay(2500).hide(500); 
                }   
               
            }  
        }); // end AJAX upita  
        $("body").css("cursor", "auto");
        $("#register_submit").attr('disabled',false);
    }
    
    
    // EDIT USER
    $( "#profile-form" ).validate({
     rules: {
        // simple rule, converted to {required:true}
         
         'user[email]'      : {
                                required : true,
                                email: true,
                                remote: {
                                            url: BASE_URL+"ajax/check_email",
                                            type: "post",
                                            data: {
                                                    email: function() {
                                                                  return $( "#reg_email" ).val();
                                                              },
                                                    user_id: function(){
                                                                  return $( "$user_id" ).val();
                                                              }
                                                   }
                                                   
                                            }
                             }, 
        
        'user[repeat_password]' : {                                      
                                      equalTo: "#password1"
                                  },
        'user[birthdate][day]'   : "required",
        'user[birthdate][month]' : "required",
        'user[birthdate][year]'  : "required",
        'user[location]'         : "required"
        },
        //*
     messages:{
         
         'user[email]'     : {
                               required: "",                              
                               remote: "Dirección de correo ya existe"
                             },
         
         'user[repeat_password]'  : "",
         'user[birthdate][day]'   : "",
         'user[birthdate][month]' : "",
         'user[birthdate][year]'  : "",
         'user[location]'         : ""
        },
        //*/
     submitHandler: function() {                                     
            register_user();
            return false;
        },
     invalidHandler: function() { } 
    });
    
});




